package com.epalmpay.service.datareport.impl;

import com.epalmpay.dto.datareport.TickDataReportInDTO;
import com.epalmpay.dto.datareport.TickDataReportListOutDTO;
import com.epalmpay.dto.datareport.TickDataReportOutDTO;
import com.epalmpay.entity.OrderServiceGood;
import com.epalmpay.entity.Ticket;
import com.epalmpay.entity.TradeOffline;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.mapper.OrderServiceGoodMapper;
import com.epalmpay.mapper.TicketMapper;
import com.epalmpay.mapper.TradeOfflineMapper;
import com.epalmpay.service.datareport.ITickDataReportService;
import com.epalmpay.util.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/3/22.
 */
@Service("tickDataReportService")
public class TickDataReportServiceImpl implements ITickDataReportService{

    @Resource
    private TicketMapper ticketMapper;

    @Resource
    private OrderServiceGoodMapper orderServiceGoodMapper;

    @Resource
    private TradeOfflineMapper tradeOfflineMapper;

    @Override
    public TickDataReportListOutDTO getTickList(TickDataReportInDTO tickDataReportInDTO) throws ApplicationException {
        TickDataReportListOutDTO tickDataReportListOutDTO = new TickDataReportListOutDTO();
        int year = Integer.parseInt(tickDataReportInDTO.getSearchkey().split("-")[0]);
        int month = Integer.parseInt(tickDataReportInDTO.getSearchkey().split("-")[1]);

        //判断是否是本月
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = dateFormat.format(new Date());
        if(tickDataReportInDTO.getSearchkey().equals(date.substring(0,7))){
            tickDataReportInDTO.setSearchkey(date);
            tickDataReportInDTO.setDays(Integer.parseInt(date.substring(8,10)));
        }else{
            tickDataReportInDTO.setSearchkey(tickDataReportInDTO.getSearchkey()+"-01");
        }

        OrderServiceGood orderServiceGood =orderServiceGoodMapper.queryTickData(tickDataReportInDTO);
        tickDataReportListOutDTO.setOrderServiceGood(orderServiceGood);
//        TradeOffline tradeOffline = tradeOfflineMapper.getTickData(tickDataReportInDTO);


        List<Ticket> ticketList = ticketMapper.getTickList(tickDataReportInDTO);
        tickDataReportListOutDTO.setTicketList(ticketList);


        List resultlist = new ArrayList();
//        Map map = new HashMap();
        if (ticketList != null) {
            for(int j=tickDataReportInDTO.getDays();j>0;j--) {
                Map resultmap = new HashMap();
                resultmap.put("datet", year + "-" + (month<10?"0"+month:month) + "-" + (j<10?"0"+j:j));
                List tickets = new ArrayList();
                for (int i = 0; i < ticketList.size(); i++) {
                    Map map = new HashMap();
                    map.put("tickname",ticketList.get(i).getTicketname());
                    map.put("tickcount",0);
                    tickets.add(map);

                }
                resultmap.put("tickets", tickets);
                resultmap.put("price",0);
                resultmap.put("number",0);
                resultmap.put("operatorName","");
                resultmap.put("inNumber",0);
                resultlist.add(resultmap);
            }
//            for (int i = 0; i < ticketList.size(); i++) {
//                map.put(ticketList.get(i).getTicketname(),0);
//            }
        }




        List<TickDataReportOutDTO> tickDataReportOutDTOList =tradeOfflineMapper.queryTickDataList(tickDataReportInDTO);
        for(TickDataReportOutDTO tickDataReportOutDTO :tickDataReportOutDTOList){
            String datet =dateFormat.format(tickDataReportOutDTO.getGmtCreate());
            for(int k=0;k<resultlist.size();k++) {
                Map map = (HashMap) resultlist.get(k);
                if(map.containsValue(datet)) {//已经存在该日期
                    System.out.print(datet);
                    List tickets = (List) map.get("tickets");
                    if(tickDataReportOutDTO.getTickName() !=null) {

                        String[] strings = tickDataReportOutDTO.getTickName().split("、");

                        for (int i = 0; i < strings.length; i++) {
                            if (strings[i] != null && !"".equals(strings[i])) {
                                for (int j = 0; j < strings[i].length(); j++) {
                                    if (strings[i].charAt(j) >= 48 && strings[i].charAt(j) <= 57) {
                                    } else {
                                        //strings[i].substring(0, j);
                                        //strings[i].substring(j);
                                        //                                if(map.containsValue(datet)) {//已经存在该日期
                                        //票类型
                                        String tickettype = strings[i].substring(j).toString();
                                        //张数
                                        int b = Integer.parseInt(strings[i].substring(0, j));
                                        for (int m = 0; m < tickets.size(); m++) {
                                            Map pmap = (Map) tickets.get(m);
                                            if (pmap.containsValue(tickettype)) {
                                                //获取当前票数
                                                int tickcount = Integer.parseInt(pmap.get("tickcount").toString());
                                                pmap.put("tickcount", tickcount + b);
                                            }
                                        }
                                        map.put("number", Integer.parseInt(map.get("number").toString()) + b);
                                        if(tickDataReportOutDTO.getOrderStatus()==8){
                                            map.put("inNumber", Integer.parseInt(map.get("inNumber").toString()) + b);
                                        }

                                        break;
                                    }
                                }
                            }
                        }
                        map.put("price", Integer.parseInt(map.get("price").toString()) + tickDataReportOutDTO.getPrice());
                        if (tickDataReportOutDTO.getOperatroName() == null) {
                            map.put("operatorName", "");
                        } else {

                            map.put("operatorName", tickDataReportOutDTO.getOperatroName());
                        }
                    }

                }

            }
        }



        tickDataReportListOutDTO.setTickDataReportOutDTOList(resultlist);



        return tickDataReportListOutDTO;
    }
}
